package com.pmcc.cj.service;

import com.pmcc.cj.entity.CjRule;
import com.pmcc.cj.repository.CjRuleRepository;
import com.pmcc.utils.AntdPage;
import com.pmcc.utils.ResultBean;
import com.pmcc.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tomas on 2020/10/15.
 */
@Transactional
@Service
public class CjRuleService {
    @Autowired
    CjRuleRepository cjRuleRepository;

    public ResultBean queryRuleByPage(AntdPage antdPage, CjRule model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Page page=cjRuleRepository.findAll(this.getSpecification(model),antdPage.getPageRequest());
            resultBean.setPage(antdPage.getDefaultCurrent());
            resultBean.setTotal(page.getTotalElements());
            resultBean.setPageSize(antdPage.getPageSize());
            resultBean.setRows(page.getContent());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public Specification<CjRule> getSpecification(CjRule model) {
        return new Specification<CjRule>() {
            @Override
            public Predicate toPredicate(Root<CjRule> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
//                if(StringUtil.isNotEmpty(model.getTitleTrain())){
//                    list.add(builder.like(root.get("titleTrain").as(String.class),  "%"+model.getTitleTrain().trim()+"%"));
//                }
//                if(model.getFettle() != null){
//                    list.add(builder.equal(root.get("fettle").as(Integer.class),  model.getFettle()));
//                }
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }
    public ResultBean addRule(CjRule model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            model.setCreateTime(new Date());
            model.setChangeTime(new Date());
            cjRuleRepository.saveAndFlush(model);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean deleteRule(String id){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            cjRuleRepository.delete(id);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean modifyRule(String id,String money,String saleMoney){
        ResultBean resultBean = new ResultBean(ResultBean.FAIL);
        try {
            CjRule rule = cjRuleRepository.findOne(id);
            if(rule != null){
                rule.setMoney(new BigDecimal(money));
                rule.setSaleMoney(new BigDecimal(saleMoney));
                rule.setChangeTime(new Date());
                cjRuleRepository.saveAndFlush(rule);
                resultBean.setResultCode(ResultBean.SUCCESS);
            }
        }catch (Exception e){
        }
        return resultBean;
    }
}
